package cn.tedu.zc.controller;

import cn.tedu.zc.mapper.UserMapper;
import cn.tedu.zc.pojo.dto.UserDTO;
import cn.tedu.zc.pojo.dto.UserLoginDTO;
import cn.tedu.zc.pojo.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    /*检查用户名重名*/
    @RequestMapping("/checkUsername")
    public int checkUsername(String username) {
        String u = mapper.checkUsername(username);
        if (u == null) {
            return 0;
        }
        return 1;
    }

    /*检查手机号重复*/
    @RequestMapping("/checkPhone")
    public int checkPhone(String phone) {
        String p = mapper.checkPhone(phone);
        if (p == null) {
            return 0;
        }
        return 1;
    }

    @RequestMapping("/getCheckNum")
    public int getCheckNum(String phone) {
        if (!phone.matches("^1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$")) {
            return 2;
        }
        if (checkPhone(phone) != 0) {
            return 1;
        }
        //TODO 调用外部接口,发送注册短信,
        // 并收到接口发送的返回值(验证码)
        // 存储到某一个对象中
        /*此处缺少调用:发送短信接口*/
        return 0;
    }

    @RequestMapping("/regUser")
    public int regUser(@RequestBody UserDTO user) {
        //TODO 此处待实现:用户申请注册时将信息
        // 提交过来,并对验证码进行校验
        System.out.println("user = " + user);
        /*用户名校验*/
        if (user.getUsername() == "" || user.getUsername() == null) {
            return 100;//用户名为空
        } else if (mapper.checkUsername(user.getUsername()) != null) {
            return 101;//用户名重复
        }
        /*手机号校验*/
        if (!user.getPhone().matches("^1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$")) {
            return 200;//手机号格式不正确
        } else if (mapper.checkPhone(user.getPhone()) != null) {
            return 201;//手机号已注册
        }
        if (!user.getCheckBoxes()) {
            return 300;//没有勾选用户协议
        }
        /*400:验证码错误*/
        mapper.insertUser(user);
        return 0;
    }

    @RequestMapping("/loginUser")
    public int loginUser(@RequestBody UserLoginDTO userLoginDTO, HttpSession session, HttpServletResponse response) {
        UserVO u = mapper.selectUserByUsername(userLoginDTO.getUsername());
        if (u != null) {
            if (u.getPassword().equals(userLoginDTO.getPassword())) {
                if (userLoginDTO.getCheckBox()) {
                    Cookie c1 = new Cookie("username", userLoginDTO.getUsername());
                    Cookie c2 = new Cookie("password", userLoginDTO.getPassword());
                    c1.setMaxAge(60 * 60 * 24 * 7);
                    response.addCookie(c1);
                    response.addCookie(c2);
                }
                session.setAttribute("user", u);
                System.out.println("userLoginDTO = " + userLoginDTO + ", session = " + session + ", response = " + response);
                return 0;
            }
            return 1;
        }
        return 2;
    }
}
